package com.vic.Builder;

import com.vic.Utils.StringUtils;
import com.vic.bean.constant;
import com.vic.bean.fieldInfo;
import com.vic.bean.tableInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Author: Vic Ly
 */
public class builderMapper {
    private static final Logger log = LoggerFactory.getLogger(builderMapper.class);
    public static void execute(tableInfo tableInfo){
        File folder = new File(constant.MAPPER_PATH);
        if(!folder.exists()){
            folder.mkdirs();
        }
        String mapperClassName = tableInfo.getBeanName()+constant.SUFFIX_MAPPER;
        File poFile = new File(folder,  mapperClassName+".java");
        // 接下来对生成的文件写入内容
        OutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(poFile);
            osw = new OutputStreamWriter(out,"utf8");
            bw = new BufferedWriter(osw);

            bw.write("package "+constant.MAPPER_PACKAGE+";");
            bw.newLine();
            bw.write("import "+constant.MAPPER_BASE_PACKAGE+".BaseMapper;");
            bw.newLine();
            bw.write("import org.apache.ibatis.annotations.Param;");
            bw.newLine();
            // 构建类注释
            builderComment.addCommentByClass(bw,tableInfo.getComment());
            bw.newLine();
            bw.write("public interface "+ mapperClassName + "<T, P> extends BaseMapper {");
            bw.newLine();
            // mapper接口中的方法
            Map<String, List<fieldInfo>> keyIndexMap = tableInfo.getKeyIndexMap();
            for(Map.Entry<String,List<fieldInfo>> entry : keyIndexMap.entrySet()){
                List<fieldInfo> keyFieldInfos = entry.getValue();
                Integer index = 0;
                StringBuilder methodName = new StringBuilder();
                StringBuilder methodParam = new StringBuilder();
                for(fieldInfo fieldInfo : keyFieldInfos){
                    index++;
                    methodName.append(StringUtils.upperCaseFirstLetter(fieldInfo.getPropertyName()));
                    if(index < keyFieldInfos.size()){
                        methodName.append("And");
                    }
                    methodParam.append("@Param(\""+fieldInfo.getPropertyName()+"\") "+fieldInfo.getJavaType()+" "+fieldInfo.getPropertyName());
                    if(index < keyFieldInfos.size()){
                        methodName.append(", ");
                    }
                }
                // 查询
                builderComment.addCommentByField(bw,"根据" + methodName + "查询");
                bw.write("\tT selectBy" + methodName +"("+methodParam+");");
                bw.newLine();
                bw.newLine();
                // 更新
                builderComment.addCommentByField(bw,"根据" + methodName + "更新");
                bw.write("\tInteger updateBy" + methodName +"(@Param(\"bean\")T t, "+methodParam+");");
                bw.newLine();
                bw.newLine();
                // 删除
                builderComment.addCommentByField(bw,"根据" + methodName + "删除");
                bw.write("\tInteger deleteBy" + methodName +"("+methodParam+");");
                bw.newLine();
                bw.newLine();
            }
            bw.write("}");
            bw.flush();
        }catch (Exception e){
            log.error("创建mapper文件失败",e);
        }finally {
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
